package com.pj.hellomvvm.ui.fragment.widget

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.pj.common.annotation.LayoutParam
import com.pj.common.base.BaseViewModel
import com.pj.common.base.topbar.BaseTopBarFragment
import com.pj.hellomvvm.R
import com.pj.hellomvvm.databinding.FragmentPullLayoutRefreshLoadBinding
import com.pj.hellomvvm.databinding.FragmentWidgetButtonBinding
import com.qmuiteam.qmui.widget.pullLayout.QMUIPullLayout
import java.util.*

@LayoutParam(R.layout.fragment_pull_layout_refresh_load, true, R.string.title_fragment_pull_refresh_load)
class PullLayoutRefreshLoadFragment :
    BaseTopBarFragment<BaseViewModel, FragmentPullLayoutRefreshLoadBinding>() {
    private var mAdapter: BaseQuickAdapter<String?,BaseViewHolder>? = null
    override fun initData(view: View, savedInstanceState: Bundle?) {
        super.initData(view, savedInstanceState)
        mCBinding.apply {
            pullLayout.setActionListener(QMUIPullLayout.ActionListener { pullAction ->
                pullLayout.postDelayed(Runnable {
                    if (pullAction.pullEdge == QMUIPullLayout.PULL_EDGE_TOP) {
                        onRefreshData()
                    } else if (pullAction.pullEdge == QMUIPullLayout.PULL_EDGE_BOTTOM) {
                        onLoadMore()
                    }
                    pullLayout.finishActionRun(pullAction)
                }, 3000)
            })

            recyclerView.setLayoutManager(object : LinearLayoutManager(context) {
                override fun generateDefaultLayoutParams(): RecyclerView.LayoutParams {
                    return RecyclerView.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT
                    )
                }
            })

            mAdapter = object : BaseQuickAdapter<String?,BaseViewHolder>(android.R.layout.simple_list_item_1) {
                override fun convert(holder: BaseViewHolder, item: String?) {
                    holder.setText(android.R.id.text1,item)
                }

            }
            mAdapter?.setOnItemClickListener(object : OnItemClickListener{
                override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, pos: Int) {
                    Toast.makeText(context, "click position=$pos", Toast.LENGTH_SHORT).show()
                }

            })
            recyclerView.setAdapter(mAdapter)
        }

        onDataLoaded()
    }

    private fun onDataLoaded() {
        val data: List<String?> = ArrayList(
            Arrays.asList(
                "Helps",
                "Maintain",
                "Liver",
                "Health",
                "Function",
                "Supports",
                "Healthy",
                "Fat",
                "Metabolism",
                "Nuturally",
                "Bracket",
                "Refrigerator",
                "Bathtub",
                "Wardrobe",
                "Comb",
                "Apron",
                "Carpet",
                "Bolster",
                "Pillow",
                "Cushion"
            )
        )
        Collections.shuffle(data)
        mAdapter?.setList(data)
    }

    private fun onRefreshData() {
        val data: MutableList<String> = ArrayList()
        val id = System.currentTimeMillis()
        for (i in 0..9) {
            data.add("onRefreshData-$id-$i")
        }
        mAdapter?.setList(data)
        mCBinding.recyclerView.scrollToPosition(0)
    }

    private fun onLoadMore() {
        val data: MutableList<String> = ArrayList()
        val id = System.currentTimeMillis()
        for (i in 0..9) {
            data.add("onLoadMore-$id-$i")
        }
        mAdapter?.addData(data)
    }
}